package com.spring.demo.springdemo.config;

import com.spring.demo.springdemo.mapper.UserMapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @Author RenPu
 * @Date 2024/2/18 14:43
 * @Version 1.0
 * @Description: 根据代理对象生成BeanDefinition对象，并且注册到BeanDefinitionRegistry中供spring使用
 *
 *
 **/
//@Component
public class MybatisDefinitioPostProcess implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //扫描指定的包路径，实现根据接口类型灵活生成BeanDefinition对象
        AbstractBeanDefinition userBeanDefinition =   BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        userBeanDefinition.setBeanClass(MybatisFactoryBeanConf.class);
        //此处为MybatisFactoryBeanConf中的mapperInterface赋值，利用指定构造方法的形式生成对应的代理对象
        userBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
        registry.registerBeanDefinition("userMapper",userBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
